package com.fancy.learncenter.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.andview.refreshview.XRefreshView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.ShowVideoOrAudioDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.adapter.FragmentShowAudioAdapter;
import com.fancy.learncenter.ui.view.CustomFootView;
import com.fancy.learncenter.ui.view.CustomHeaderView;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowAudioFragment extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.xRefreshView)
    XRefreshView xRefreshView;

    private List<ShowVideoOrAudioDataBean.RecommendListBean> listData;
    private FragmentShowAudioAdapter listAdapter;

    private int curPage = 1;
    private int pageSize = 3;

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_show_audio, container, false);
        ButterKnife.bind(this, rootView);

        initRecycleView();

        getData(true);

        return rootView;
    }

    private void initRecycleView() {
        listData = new ArrayList<>();

        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        recyclerView.setLayoutManager(manager);

        listAdapter = new FragmentShowAudioAdapter(getActivity(), listData);
        recyclerView.setAdapter(listAdapter);
        listAdapter.setCustomLoadMoreView(new CustomFootView(getActivity()));
        xRefreshView.setPullLoadEnable(true);
        xRefreshView.setCustomHeaderView(new CustomHeaderView(getActivity()));
//        xRefreshView.setXRefreshViewListener(new XRefreshView.XRefreshViewListener() {
//            @Override
//            public void onRefresh() {
//                getData(true);
//            }
//
//            @Override
//            public void onRefresh(boolean isPullDown) {
//
//            }
//
//            @Override
//            public void onLoadMore(boolean isSilence) {
//                getData(false);
//            }
//
//            @Override
//            public void onRelease(float direction) {
//
//            }
//
//            @Override
//            public void onHeaderMove(double headerMovePercent, int offsetY) {
//
//            }
//        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void getData(final boolean refresh) {
        Map<String, Object> paramer = new HashMap<>();
        paramer.put("type", "audio");

        if (refresh) {
            curPage = 1;
        } else {
            curPage++;
        }
        paramer.put("curPage", curPage);
        paramer.put("pageSize", pageSize);

        HttpMehtod.getInstance().getShowListData(paramer, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<ShowVideoOrAudioDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), ShowVideoOrAudioDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<ShowVideoOrAudioDataBean>() {
                    @Override
                    public void success(ShowVideoOrAudioDataBean dataBean) {
                        if (refresh) {
                            listAdapter.notifyDataSetChanged(dataBean.getRecommendList());
                            xRefreshView.stopRefresh();
                            xRefreshView.setLoadComplete(false);
                        } else {
                            listAdapter.addAll(dataBean.getRecommendList());
                            if (dataBean.getRecommendList() != null) {
                                if (dataBean.getRecommendList().size() < pageSize) {
                                    xRefreshView.setLoadComplete(true);
                                } else {
                                    xRefreshView.stopLoadMore();
                                }
                            } else {
                                xRefreshView.setLoadComplete(true);
                            }
                        }
                    }

                    @Override
                    public void fail(String errorCode) {

                    }


                }).dealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                xRefreshView.stopRefresh();

                xRefreshView.stopLoadMore();
            }
        });
    }

}
